<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <!-- <el-form-item label="" prop="code">
        <el-input v-model="dataForm.code" placeholder=""></el-input>
      </el-form-item> -->
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择类型">
          <el-option label="公司行业" value="1"></el-option>
          <el-option label="产品类别" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="英文名称" prop="eName">
        <el-input v-model="dataForm.eName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="父类别id" prop="pid">
        <el-input v-model="dataForm.pid" placeholder="父类别id"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="sysState">
        <el-input v-model="dataForm.sysState" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="展会id" prop="exhId">
        <el-input v-model="dataForm.exhId" placeholder="展会id"></el-input>
      </el-form-item>
      <el-form-item label="爷类别id" prop="grandPid">
        <el-input v-model="dataForm.grandPid" placeholder="爷类别id"></el-input>
      </el-form-item>
      <el-form-item label="展会项目id" prop="epjId">
        <el-input v-model="dataForm.epjId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="问题id" prop="ansId">
        <el-input v-model="dataForm.ansId" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  code: "",
  type: "",
  name: "",
  eName: "",
  sort: "",
  pid: "",
  sysState: "",
  remark: "",
  exhId: "",
  grandPid: "",
  epjId: "",
  ansId: "",
  creator: "",
  createTime: "",
  updater: "",
  updateTime: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/crm/crmtypeinfo/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/crm/crmtypeinfo", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
